package cn.zhizihua.controller;

import cn.zhizihua.pojo.Business;
import cn.zhizihua.query.BusinessQuery;
import cn.zhizihua.service.IBusinessService;
import cn.zhizihua.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/business")   //模块路径
public class BusinessController {

    @Autowired
    IBusinessService businessService;

    @PostMapping      //  请求方式 + 请求路径   post  /business
    public ResultBean save(@RequestBody Business business){
        System.out.println("----BusinessController-----save--------");
        System.out.println(business);
        if (business.getBusinessId()==null){
            return businessService.add(business);
        }
        return businessService.update(business);
    }

    /*@PostMapping      //  请求方式 + 请求路径   post  /business
    public ResultBean add(@RequestBody Business business){
        System.out.println("----BusinessController-----add--------");
        System.out.println(business);
        return businessService.add(business);
    }

    @PutMapping    //   put   /business
    public ResultBean update(@RequestBody Business business){
        System.out.println("-------BusinessController--------update-------------");
        System.out.println(business);
        return businessService.update(business);
    }*/

    @DeleteMapping("/{id}")   // http://localhost:8080/business/1003   //  delete   /business/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------BusinessController--------del-------------");
        System.out.println(id);
        return businessService.delete(id);
    }

    @GetMapping("/{id}")   // http://localhost:8080/business/1003   //  get   /business/1004
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------BusinessController--------getById-------------");
        System.out.println(id);
        return businessService.getById(id);
    }

    @GetMapping("/all")  //get /business/all
    public ResultBean getAll(){
        System.out.println("-------BusinessController--------getAll-------------");
        return businessService.getAll();
    }

    @PostMapping("/page")   // post  /business/page
    public ResultBean getByPage(@RequestBody BusinessQuery businessQuery){
        System.out.println("-------BusinessController--------getByPage-------------");
        System.out.println(businessQuery);
        return businessService.getByPage(businessQuery);
    }

    @PostMapping("/batch/{ids}")   // post + /business/batch
    public ResultBean batchDel(@PathVariable("ids") String ids){
        System.out.println("----------BusinessController---------batchDel---------------");
        System.out.println(ids); //接收到的参数格式 ： 21,19
        String[] strIds = ids.split(",");  //字符串拆分  [21,19]
        System.out.println(Arrays.toString(strIds));
        List<Integer> numids = new ArrayList<>();  //创建一个Integer集合  service层需要的参数类型
        for (String strid: strIds) {
            numids.add(Integer.parseInt(strid));//将数组中的每一项转型为Integer再加入到集合中
        }
        System.out.println(numids);
        return businessService.batchDel(numids);
    }
}
